package com.order.controller.business;  

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Record;
import com.order.controller.BaseController;
import com.order.config.Consts;
import com.order.interceptor.business.AccountInterceptor;
import com.order.model.Account;
import com.order.model.OrderHead;
import com.order.model.OrderLine;


/** 
 * @Title: OrdreController.java 
 * @Package com.order.controller 
 * @Description: 点餐控制器
 * @author LinBo
 * @date 2014-3-27 下午09:25:22 
 */

@Before(AccountInterceptor.class)
@ControllerBind(controllerKey= Consts.BUSINESS + "order")
public class OrderController extends BaseController {
	
	/** 
	* @Title: toOrderHistory 
	* @Description:    定位我的订单历史
	*/ 
	public void toOrderHistory(){
		Account account = getSessionAttr(Consts.ACCOUNT);
		System.out.println(account);
		List<Record> list = OrderHead.dao.getAllOrder(account.get(Account.ID).toString());
		setAttr("orders", list);
		//setAttr("total", list.get(0).get("total"));
		render(Consts.BUSINESS + "order.htm");
	}
	
	
	/** 
	* @Title: createOrder 
	* @描述:    下单
	*/ 
	public void createOrder(){
		int i = 1;
		String cuisineDishId = getPara("cuisine_dish_id1");
		String num = getPara("num1");
		List<String> ids = new ArrayList<String>();
		List<String> nums = new ArrayList<String>();
		while(StringKit.notBlank(cuisineDishId)){
			System.out.println(" -----------------------判断前："+num);
			if(!"0".equals(num) && num != null){
				ids.add(cuisineDishId);
				nums.add(num);
			}
			i++;
			cuisineDishId = getPara("cuisine_dish_id" + i);
			num = getPara("num" + i);
			System.out.println(" -----------------------获取后："+num);
		}
		String reciver = getPara("reciver","");
		String address = getPara("address","");
		String phone = getPara("phone","");
		Account account = getSessionAttr(Consts.ACCOUNT);
		if(StringKit.notBlank(reciver)){
			account.set(Account.NAME, reciver);
		}
		if(StringKit.notBlank(phone)){
			account.set(Account.PHONE, phone);
		}
		if(StringKit.notBlank(address)){
			account.set(Account.ADDRESS, address);
		}
		if(!ids.isEmpty()  && !nums.isEmpty()){
			OrderHead.dao.createOrder(account, ids, nums);
		}
		//清除购物车
		setSessionAttr(Consts.CART, null);
		redirect(Consts.BUSINESS + "order/toOrderHistory");
	}
	
	/** 
	* @Title: doEvaluation 
	* @描述:    评价菜品
	*/ 
	public void doEvaluation(){
		String orderLineId = getPara("order_line_id");
		String evaluation = getPara("evaluation");
		String grade = getPara("grade");
		OrderLine.dao.doEvaluation(orderLineId, evaluation, grade);
		redirect(Consts.BUSINESS + "order/toOrderHistory");
	}
	
	public void confirmRecive(){
		String orderLineId = getPara("order_line_id");
		String msg = OrderHead.dao.confirmRecive(orderLineId);
		setAttr("msg", msg);
		renderJson();
	}
}
  